package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.PriceListBean;
import com.aso114.project.bean.UserInfoBean;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SystemBarHelper;
import com.aso114.project.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VipCoreActivity extends BaseSimpleActivity {


    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    @BindView(R.id.vip_no_ly)
    ConstraintLayout vipNoLy;
    @BindView(R.id.vip_yes_ly)
    ConstraintLayout vipYesLy;
    public static String is_vip = "";
    Intent intent;
    @BindView(R.id.vip_core_title)
    TextView vipCoreTitle;
    @BindView(R.id.vip_name)
    TextView vipName;
    @BindView(R.id.vip_img)
    ImageView vipImg;
    @BindView(R.id.vip_time)
    TextView vipTime;
    @BindView(R.id.vip_core_price)
    TextView vipCorePrice;

    private List<PriceListBean> priceListBean = new ArrayList<>();// （遍历后的数据）方便与每月最低价格的展示
    private double minPrice = 10000;//默认一个最低价格（要大于每月价格才好替换）

    @Override
    protected int getResouseId() {
        return R.layout.activity_vipcore;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        layoutTitleBarTitleTv.setText("开通会员");
        vipCoreTitle.setText(getString(R.string.app_name) + "VIP");
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();

    }

    private void getUserInfo() {
        CommentModel.getInstant().getUserInfo(new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                UserInfoBean loginBean = (UserInfoBean) o;
                if (loginBean == null) {
                    return;
                }
                if ("1".equals(loginBean.getIsVip())) { //  是VIP
                    vipYesLy.setVisibility(View.VISIBLE);
                    vipNoLy.setVisibility(View.GONE);
                } else {
                    vipYesLy.setVisibility(View.GONE);
                    vipNoLy.setVisibility(View.VISIBLE);
                }
                if (!TextUtils.isEmpty(loginBean.getUserName())) {
                    vipName.setText(loginBean.getUserName());
                } else {
                    vipName.setText(loginBean.getUserMobile());
                }
                vipTime.setText(Helper.getCurrentTime(Long.valueOf(loginBean.getVipEndTime())));
            }

            @Override
            public void onFailure(String fail) {
                ToastUtils.showToast(VipCoreActivity.this, fail);
            }
        });

        CommentModel.getInstant().getPriceList(new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆
                List<PriceListBean> baseBean = (List<PriceListBean>) o;
                if (baseBean == null || baseBean.isEmpty()) {
                    return;
                }
                for (int i = 0; i < baseBean.size(); i++) {
                    if ("1".equals(baseBean.get(i).getIsShow())) {
                        priceListBean.add(baseBean.get(i));
                    }
                }
                for (int p = 0; p < priceListBean.size(); p++) {
                    double price = priceListBean.get(p).getVipPrice() / (priceListBean.get(p).getVipNum() / 30);
                    if (minPrice > price) {
                        minPrice = price;
                    }
                }
                vipCorePrice.setText(String.format("%.2f", minPrice));
            }

            @Override
            public void onFailure(String fail) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.vip_qkt, R.id.vip_xf})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.vip_qkt:
            case R.id.vip_xf:
                intent = new Intent(VipCoreActivity.this, OpenVipActivity.class);
                startActivity(intent);
                break;
        }
    }
}
